{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "cell_0",
      "metadata": {},
      "source": [
        "# Quick Start Example - Extracting concept from a document with an image"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cell_1",
      "metadata": {},
      "outputs": [],
      "source": [
        "%pip install -U contextgem"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "cell_2",
      "metadata": {},
      "source": [
        "To run the extraction, please provide your LLM details in the ``DocumentLLM(...)`` constructor further below."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cell_3",
      "metadata": {},
      "outputs": [],
      "source": [
        "# Quick Start Example - Extracting concept from a document with an image\n",
        "\n",
        "import os\n",
        "from pathlib import Path\n",
        "\n",
        "from contextgem import Document, DocumentLLM, NumericalConcept, create_image\n",
        "\n",
        "\n",
        "# Path adapted for testing\n",
        "current_file = Path(__file__).resolve()\n",
        "root_path = current_file.parents[4]\n",
        "image_path = root_path / \"tests\" / \"images\" / \"invoices\" / \"invoice.jpg\"\n",
        "\n",
        "# Create an image instance using the create_image utility\n",
        "doc_image = create_image(image_path)\n",
        "\n",
        "# Example document instance holding only the image\n",
        "doc = Document(\n",
        "    images=[doc_image],  # may contain multiple images\n",
        ")\n",
        "\n",
        "# Define a concept to extract the invoice total amount\n",
        "doc_concept = NumericalConcept(\n",
        "    name=\"Invoice Total\",\n",
        "    description=\"The total amount to be paid as shown on the invoice\",\n",
        "    numeric_type=\"float\",\n",
        "    llm_role=\"extractor_vision\",  # use vision model\n",
        ")\n",
        "\n",
        "# Add concept to the document\n",
        "doc.add_concepts([doc_concept])\n",
        "# (add more concepts to the document, if needed)\n",
        "\n",
        "# Create an LLM for extraction\n",
        "llm = DocumentLLM(\n",
        "    model=\"openai/gpt-4o-mini\",  # Using a model with vision capabilities\n",
        "    api_key=os.environ.get(\"CONTEXTGEM_OPENAI_API_KEY\"),  # your API key\n",
        "    role=\"extractor_vision\",  # mark LLM as vision model\n",
        ")\n",
        "\n",
        "# Extract information from the document\n",
        "extracted_concepts = llm.extract_concepts_from_document(doc)\n",
        "# or use async version: await llm.extract_concepts_from_document_async(doc)\n",
        "\n",
        "# Access extracted information\n",
        "print(\"Invoice Total:\")\n",
        "print(extracted_concepts[0].extracted_items)  # extracted concept items\n",
        "# or doc.get_concept_by_name(\"Invoice Total\").extracted_items\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.10.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}